Image Processing

CIS 193 – Go Programming

Prakhar Bhandari, Adel Qalieh

CIS 193

Course Logistics

Image packages

Go Standard Library

Blank imports - remember these?

import _ "image/png"

Creating a Simple Image

package main

import "image"
import "image/color"
import "image/png"
import "os"

func main() {
    img := image.NewRGBA(image.Rect(0, 0, 100, 100))

    // Color the pixel at 5, 5 green
    img.Set(2, 3, color.RGBA{0, 250, 0, 255})

    // Save to image.png
    f, _ := os.Create("image.png")
    defer f.Close()
    png.Encode(f, img)
}

Creating Images Demo

Creating a GIF

package main

import "image"
import "image/gif"
import "image/png"
import "os"

func main() {
    files := []string{"img1.png", "img2.png","img3.png", "img2.png"}

    outGif := &gif.GIF{}
    for _, name := range files {
        f, _ := os.Open(name)
        img, _ := png.Decode(f)
        f.Close()

        outGif.Image = append(outGif.Image, img.(*image.Paletted))
        outGif.Delay = append(outGif.Delay, 0)
    }

    f, _ := os.Create("output.gif")
    defer f.Close()
    gif.EncodeAll(f, outGif)
}

Creating GIF Demo

Go Image Processing Packages

The Go standard library is amazing, but it's often easier to use external packages

Go Image Libraries

Making Memes

Debugging

Inspecting your program for bugs, usually line by line. Other languages such as Java or Python have sophisticated debugging tools. What about Go?

So far, how have you been debugging?

Native Debugging with GDB

GDB is the GNU Debugger, which is intended for debugging C, C++, and Fortran programs. You can also use GDB with Go, but with many caveats. It also requires a UNIX-like environment, so Linux or MacOS. In other words, no Windows support.

See the documentation: golang.org/doc/gdb

Basic GDB Operations

gdb ./mygoexecutable

Warning: some operations and expressions will be substantially more verbose in GDB

Debugging with godebug

A few other debugging tools have sprung up, currently the 2 favorites are delve and godebug.

$ go get github.com/mailgun/godebug

Basic godebug Usage

1. Insert a breakpoint anywhere in your code

_ = "breakpoint"

2. Start program with debugger

godebug run mygofile.go

Commands very similar to GDB:

Caveats

Homework

No new homework, start working on final projects!

Thank you

Prakhar Bhandari, Adel Qalieh

CIS 193

Use the left and right arrow keys or click the left and right edges of the page to navigate between slides.
(Press 'H' or navigate to hide this message.)